package com.supor.aiot.ui.privacy;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.supor.aiot.R;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.SuporApplication;
import com.supor.aiot.common.base.BaseMVVMFragment;
import com.supor.aiot.common.base.NoViewModel;
import com.supor.aiot.databinding.FragmentSplashBinding;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: SplashFragment <br>
 * 作者: lei <br>
 * 日期: 2021/1/23 <br>
 */
public class SplashFragment extends BaseMVVMFragment<NoViewModel, FragmentSplashBinding> {
    SplashActivity activity;

    public static SplashFragment newInstance(int step) {
        Bundle args = new Bundle();
        args.putInt(Constants.Key.param_step, step);
        SplashFragment fragment = new SplashFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int setContent() {
        return R.layout.fragment_splash;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        bindingView.ivSkip.setTypeface(null, Typeface.NORMAL);
        bindingView.ivSkip.setOnClickListener(this::onViewClicked);
        bindingView.btnStart.setOnClickListener(this::onViewClicked);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle args = getArguments();
        if (args != null) {
            int step = args.getInt(Constants.Key.param_step);
            switch (step) {
                case 1:
                    bindingView.ivPage.setImageResource(R.drawable.page01);
                    bindingView.ivSkip.setVisibility(View.VISIBLE);
                    bindingView.btnStart.setVisibility(View.GONE);
                    break;
                case 2:
                    bindingView.ivPage.setImageResource(R.drawable.page02);
                    bindingView.ivSkip.setVisibility(View.VISIBLE);
                    bindingView.btnStart.setVisibility(View.GONE);
                    break;
                case 3:
                    bindingView.ivPage.setImageResource(R.drawable.page03);
                    bindingView.ivSkip.setVisibility(View.VISIBLE);
                    bindingView.btnStart.setVisibility(View.GONE);
                    break;
                case 4:
                    bindingView.ivPage.setImageResource(R.drawable.page04);
                    bindingView.ivSkip.setVisibility(View.GONE);
                    bindingView.btnStart.setVisibility(View.VISIBLE);
                    break;
                /*case 5:
                    bindingView.ivPage.setImageResource(R.drawable.page05);
                    bindingView.ivSkip.setVisibility(View.GONE);
                    bindingView.btnStart.setVisibility(View.VISIBLE);
                    break;*/
                default:
                    bindingView.ivPage.setImageResource(R.drawable.page01);
                    break;
            }
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (SplashActivity) getActivity();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_skip:
            case R.id.btn_start:
                // activity.startActivityForResult(new Intent(activity, AuthPrivacyActivity.class),
                //         Constants.FIRST_PRIVACY);
                // activity.overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
                BaseSharedPreferences.setInt(SuporApplication.getInstance(),
                        Constants.Key.param_firstOpenApp, 0);
                // activity.finish();
                activity.setResult(1);
                activity.finish();
                activity.overridePendingTransition(0, 0);
                break;
        }
    }
}
